#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app import create_app
from models import db, Problem
import json

def add_more_problems():
    """添加更多题目到题库"""
    app = create_app()
    
    with app.app_context():
        # 定义新的题目数据
        new_problems_data = [
            {
                "title": "快速排序",
                "description": """实现快速排序算法。快速排序是一种高效的排序算法，使用分治策略。

快速排序的基本思想是：
1. 选择一个基准元素（通常是最后一个元素）
2. 将数组分为两部分，左边都小于基准，右边都大于基准
3. 递归对左右两部分进行排序

示例：
输入：[64, 34, 25, 12, 22, 11, 90]
输出：[11, 12, 22, 25, 34, 64, 90]

输入：[5, 2, 4, 6, 1, 3]
输出：[1, 2, 3, 4, 5, 6]""",
                "type": "code",
                "answer": None,
                "test_cases": json.dumps([
                    {
                        "input": "[64, 34, 25, 12, 22, 11, 90]",
                        "output": "[11, 12, 22, 25, 34, 64, 90]"
                    },
                    {
                        "input": "[5, 2, 4, 6, 1, 3]",
                        "output": "[1, 2, 3, 4, 5, 6]"
                    }
                ]),
                "difficulty": "hard",
                "created_by": 1
            },
            {
                "title": "链表反转",
                "description": """反转一个单链表。

示例：
输入：1->2->3->4->5->NULL
输出：5->4->3->2->1->NULL

输入：1->2->NULL
输出：2->1->NULL

输入：NULL
输出：NULL""",
                "type": "code",
                "answer": None,
                "test_cases": json.dumps([
                    {
                        "input": "[1,2,3,4,5]",
                        "output": "[5,4,3,2,1]"
                    },
                    {
                        "input": "[1,2]",
                        "output": "[2,1]"
                    },
                    {
                        "input": "[]",
                        "output": "[]"
                    }
                ]),
                "difficulty": "medium",
                "created_by": 1
            },
            {
                "title": "二叉树遍历",
                "description": """实现二叉树的前序遍历。前序遍历的顺序是：根节点 -> 左子树 -> 右子树。

示例：
输入：
    1
   / \\
  2   3
 / \\
4   5

输出：[1, 2, 4, 5, 3]

输入：
    1
     \\
      2
       \\
        3

输出：[1, 2, 3]""",
                "type": "code",
                "answer": None,
                "test_cases": json.dumps([
                    {
                        "input": "[1,2,3,4,5]",
                        "output": "[1,2,4,5,3]"
                    },
                    {
                        "input": "[1,null,2,null,3]",
                        "output": "[1,2,3]"
                    }
                ]),
                "difficulty": "medium",
                "created_by": 1
            },
            {
                "title": "最长公共子序列",
                "description": """给定两个字符串 text1 和 text2，返回这两个字符串的最长公共子序列的长度。

子序列是指在不改变字符相对顺序的情况下删除某些字符（也可以不删除任何字符）后组成的新字符串。

示例：
输入：text1 = "abcde", text2 = "ace"
输出：3
解释：最长公共子序列是 "ace"，它的长度为 3。

输入：text1 = "abc", text2 = "abc"
输出：3
解释：最长公共子序列是 "abc"，它的长度为 3。

输入：text1 = "abc", text2 = "def"
输出：0
解释：两个字符串没有公共子序列，返回 0。""",
                "type": "code",
                "answer": None,
                "test_cases": json.dumps([
                    {
                        "input": '"abcde"\n"ace"',
                        "output": "3"
                    },
                    {
                        "input": '"abc"\n"abc"',
                        "output": "3"
                    },
                    {
                        "input": '"abc"\n"def"',
                        "output": "0"
                    }
                ]),
                "difficulty": "hard",
                "created_by": 1
            },
            {
                "title": "合并两个有序数组",
                "description": """给你两个按非递减顺序排列的整数数组 nums1 和 nums2，另有两个整数 m 和 n ，分别表示 nums1 和 nums2 中的元素数目。

请你合并 nums2 到 nums1 中，使合并后的数组同样按非递减顺序排列。

注意：最终，合并后数组不应由函数返回，而是存储在数组 nums1 中。为了应对这种情况，nums1 的初始长度为 m + n，其中前 m 个元素表示应合并的元素，后 n 个元素为 0 ，应忽略。nums2 的长度为 n 。

示例：
输入：nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出：[1,2,2,3,5,6]
解释：需要合并 [1,2,3] 和 [2,5,6] 。
合并结果是 [1,2,2,3,5,6] 。

输入：nums1 = [1], m = 1, nums2 = [], n = 0
输出：[1]
解释：需要合并 [1] 和 [] 。
合并结果是 [1] 。""",
                "type": "code",
                "answer": None,
                "test_cases": json.dumps([
                    {
                        "input": "[1,2,3,0,0,0]\n3\n[2,5,6]\n3",
                        "output": "[1,2,2,3,5,6]"
                    },
                    {
                        "input": "[1]\n1\n[]\n0",
                        "output": "[1]"
                    }
                ]),
                "difficulty": "easy",
                "created_by": 1
            },
            {
                "title": "Python生成器",
                "description": "Python中用于创建生成器的关键字是？",
                "type": "choice",
                "answer": "yield",
                "test_cases": json.dumps([
                    {
                        "options": ["return", "yield", "generate", "create"],
                        "correct": "yield"
                    }
                ]),
                "difficulty": "medium",
                "created_by": 1
            },
            {
                "title": "Python上下文管理器",
                "description": "Python中用于实现上下文管理器的魔术方法是？",
                "type": "choice",
                "answer": "__enter__ 和 __exit__",
                "test_cases": json.dumps([
                    {
                        "options": ["__init__ 和 __del__", "__enter__ 和 __exit__", "__start__ 和 __end__", "__open__ 和 __close__"],
                        "correct": "__enter__ 和 __exit__"
                    }
                ]),
                "difficulty": "hard",
                "created_by": 1
            },
            {
                "title": "Python多线程",
                "description": "Python中用于创建线程的模块是？",
                "type": "choice",
                "answer": "threading",
                "test_cases": json.dumps([
                    {
                        "options": ["thread", "threading", "multithread", "concurrent"],
                        "correct": "threading"
                    }
                ]),
                "difficulty": "medium",
                "created_by": 1
            },
            {
                "title": "Python虚拟环境",
                "description": "Python中用于创建虚拟环境的命令是？",
                "type": "choice",
                "answer": "python -m venv",
                "test_cases": json.dumps([
                    {
                        "options": ["python -m venv", "pip install venv", "virtualenv create", "conda create"],
                        "correct": "python -m venv"
                    }
                ]),
                "difficulty": "easy",
                "created_by": 1
            },
            {
                "title": "Python包管理",
                "description": "Python中用于安装第三方包的命令是？",
                "type": "choice",
                "answer": "pip install",
                "test_cases": json.dumps([
                    {
                        "options": ["pip install", "python install", "package install", "import install"],
                        "correct": "pip install"
                    }
                ]),
                "difficulty": "easy",
                "created_by": 1
            }
        ]
        
        # 检查题目是否已存在
        existing_titles = [p.title for p in Problem.query.all()]
        new_problems = []
        
        for problem_data in new_problems_data:
            if problem_data["title"] not in existing_titles:
                problem = Problem(**problem_data)
                db.session.add(problem)
                new_problems.append(problem_data["title"])
        
        # 提交到数据库
        db.session.commit()
        
        print(f"成功添加 {len(new_problems)} 道新题目")
        if new_problems:
            print("新增题目列表：")
            for title in new_problems:
                print(f"- {title}")
        else:
            print("没有新增题目，所有题目都已存在")

if __name__ == '__main__':
    add_more_problems()
